package com.meixiang.activity.account;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.reflect.TypeToken;
import com.meixiang.R;
import com.meixiang.activity.account.managers.myPurse.RechargeActivity;
import com.meixiang.dialog.MemberDialogFragment;
import com.meixiang.entity.account.AllPrivilegeEntity;
import com.meixiang.global.Config;
import com.meixiang.http.HttpCallBack;
import com.meixiang.http.HttpUtils;
import com.meixiang.main.BaseActivity;
import com.meixiang.recyclerview.OnItemClick;
import com.meixiang.util.AbJsonUtil;
import com.meixiang.util.AbToastUtil;
import com.meixiang.util.GlideHelper;
import com.meixiang.view.StripTypeProgressBar;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 会员卡
 * author：wangzuzhen on 2016/5/6 0006 10:33.
 */
public class MemberCardActivity extends BaseActivity {

    /**
     * 商店图片
     */
    @Bind(R.id.iv_portrait)
    ImageView mIvPortrait;
    /**
     * 商店名字
     */
    @Bind(R.id.tv_username)
    TextView mTvUsername;
    /**
     * 会员等级
     */
    @Bind(R.id.tv_member)
    TextView mTvMember;
    /**
     * 铜牌
     */
    @Bind(R.id.iv_tong_member)
    ImageView mIvTongMember;
    /**
     * 银牌
     */
    @Bind(R.id.iv_yin_member)
    ImageView mIvYinMember;
    /**
     * 金牌
     */
    @Bind(R.id.iv_jin_member)
    ImageView mIvJinMember;
    /**
     * 进度条
     */
    @Bind(R.id.member_progressbar)
    StripTypeProgressBar mMemberProgressbar;
    /**
     * 进度条textview
     */
    @Bind(R.id.tv_member_progressbar)
    TextView mTvMemberProgressbar;
    /**
     * 积分
     */
    @Bind(R.id.tv_member_text)
    TextView mTvMemberText;
    /**
     * 积分
     */
    @Bind(R.id.tv_integral)
    TextView mTvIntegral;
    @Bind(R.id.tv_member_balance)
    TextView mTvMemberBalance;
    /**
     * 可用余额
     */
    @Bind(R.id.tv_member_balance_money)
    TextView mTvMemberBalanceMoney;
    /**
     * 充值
     */
    @Bind(R.id.tv_member_recharge)
    TextView mTvMemberRecharge;
    @Bind(R.id.rl_member_balance)
    RelativeLayout mRlMemberBalance;
    /**
     * 可用积分
     */
    @Bind(R.id.tv_integrals)
    TextView mTvIntegrals;
    @Bind(R.id.tv_member_integral)
    TextView mTvMemberIntegral;
    @Bind(R.id.rl_member_integral)
    RelativeLayout mRlMemberIntegral;

    /**
     * 我的特权--查看全部
     */
    @Bind(R.id.tv_my_privilege)
    TextView mTvMyPrivilege;
    @Bind(R.id.rl_my_privilege)
    RelativeLayout mRlMyPrivilege;
    @Bind(R.id.swipe_target)
    RecyclerView mSwipeTarget;
//    /**
//     * 我的特权--优惠折扣
//     */
//    @Bind(R.id.member_rebate)
//    LayoutRipple mMemberRebate;
//    /**
//     * 我的特权--积分翻倍
//     */
//    @Bind(R.id.member_double_integral)
//    LayoutRipple mMemberDoubleIntegral;
//    /**
//     * 我的特权--免费美容
//     */
//    @Bind(R.id.member_beauty)
//    LayoutRipple mMemberBeauty;
//    /**
//     * 我的特权--推荐奖励
//     */
//    @Bind(R.id.member_bonuses)
//    LayoutRipple mMemberBonuses;
//    /**
//     * 我的特权--优先服务
//     */
//    @Bind(R.id.member_service)
//    LayoutRipple mMemberService;

    private AllPrivilegeAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_member_card);
        ButterKnife.bind(this);
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void initTitle() {
        setRightTitle("会员卡", "管理", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MemberDialogFragment.newInstance().show(getSupportFragmentManager(), MemberDialogFragment.TAG);
            }
        });
    }

    @Override
    protected void initView() {
        mAdapter = new AllPrivilegeAdapter(context);
        //设置布局管理器
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        mSwipeTarget.setLayoutManager(linearLayoutManager);
        mSwipeTarget.setAdapter(mAdapter);
        mAdapter.setOnItemClickListener(new OnItemClick() {
            @Override
            public void onItemClick(View view, int position) {

                startActivity(new Intent(context, AllPrivilegeActivity.class));
            }
        });
    }

    @Override
    protected void setData() {

        getMemberData();
    }


    private void getMemberData() {

        HttpUtils.post(Config.MEMBER_INDEX, null, new HttpCallBack(MemberCardActivity.this, "数据获取中...") {
            @Override
            public void onSucceed(JSONObject result, String code, String message) {
                if ("0".equals(code)) {
                    GlideHelper.showCircleImage(context,result.optString("storeLogo"), R.mipmap.default_head_portrait, mIvPortrait);
                    mTvUsername.setText(result.optString("storeName"));
                    mTvMember.setText(result.optString("gradeName"));

                    int progressRate = Integer.parseInt(result.optString("nowGrade"));
                    int endRate = Integer.parseInt(result.optString("endGrade"));
                    if (progressRate > endRate) {
                        progressRate = 100;
                    }

                    mMemberProgressbar.setAnimProgress(progressRate);
                    mTvMemberProgressbar.setText(result.optString("nowGrade") + "/" + result.optString("endGrade"));
                    mTvMemberText.setText(result.optString("desc"));
                    mTvMemberBalanceMoney.setText(result.optString("cardBalance") + "元");
                    mTvMemberIntegral.setText(result.optString("isPayPoint") + "元");

                    List<AllPrivilegeEntity> mList = AbJsonUtil.fromJson(result.optJSONArray("list").toString(), new TypeToken<ArrayList<AllPrivilegeEntity>>() {
                    });
                    if (mList.size() > 0) {
                        mAdapter.refreshData(mList);
                    }

                } else {
                    AbToastUtil.showToast(context, message);
                }
            }

            @Override
            public void onError(String code, String message) {
                AbToastUtil.showToast(context, message);
            }
        });
    }

    @OnClick({R.id.tv_member_recharge, R.id.rl_my_privilege/*, R.id.member_rebate, R.id.member_double_integral, R.id.member_beauty, R.id.member_bonuses, R.id.member_service*/})
    public void onClick(View view) {
//        Intent intent = new Intent();
        switch (view.getId()) {
            case R.id.tv_member_recharge:
                Intent intentPay = new Intent(context, RechargeActivity.class);
                intentPay.putExtra(RechargeActivity.RECHARGE_TYPE, RechargeActivity.CLUB_CARD_RECHARGE);
                startActivity(intentPay);
                break;
            case R.id.rl_my_privilege:
                startActivity(new Intent(context, AllPrivilegeActivity.class));
                break;
           /* case R.id.member_rebate:
                intent.setClass(context, AllPrivilegeActivity.class);
                intent.putExtra("status", "优惠折扣");
                startActivity(intent);
                break;
            case R.id.member_double_integral:
                intent.setClass(context, AllPrivilegeActivity.class);
                intent.putExtra("status", "积分翻倍");
                startActivity(intent);
                break;
            case R.id.member_beauty:
                intent.setClass(context, AllPrivilegeActivity.class);
                intent.putExtra("status", "免费美容");
                startActivity(intent);
                break;
            case R.id.member_bonuses:
                intent.setClass(context, AllPrivilegeActivity.class);
                intent.putExtra("status", "推荐奖励");
                startActivity(intent);
                break;
            case R.id.member_service:
                intent.setClass(context, AllPrivilegeActivity.class);
                intent.putExtra("status", "优先服务");
                startActivity(intent);
                break;*/
        }
    }


    public class AllPrivilegeAdapter extends RecyclerView.Adapter<AllPrivilegeAdapter.MyViewHolder> {

        private List<AllPrivilegeEntity> mAllPrivilegeList = new ArrayList<>();
        private Context context;

        public AllPrivilegeAdapter(Context context) {
            this.context = context;
        }

        /**
         * 刷新数据
         */
        public void refreshData(List<AllPrivilegeEntity> list) {
            this.mAllPrivilegeList = list;
            this.notifyDataSetChanged();
        }

        @Override
        public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            MyViewHolder myViewHolder = new MyViewHolder(LayoutInflater.from(context).inflate(R.layout.list_item_member_card, parent, false));
            return myViewHolder;
        }

        public OnItemClick onItemClickListener;

        public void setOnItemClickListener(OnItemClick onItemClickListener) {
            this.onItemClickListener = onItemClickListener;
        }

        @Override
        public void onBindViewHolder(final MyViewHolder holder, int position) {
            if (onItemClickListener != null) {
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int pos = holder.getLayoutPosition();
                        onItemClickListener.onItemClick(holder.itemView, pos);
                    }
                });
            }

            AllPrivilegeEntity allPrivilegeEntity = mAllPrivilegeList.get(position);
            if (allPrivilegeEntity != null) {
                GlideHelper.showImage(context, allPrivilegeEntity.getPhoto(), holder.mIvPicture);
                holder.mTvMsgTitle.setText(allPrivilegeEntity.getName());
            }


        }

        @Override
        public int getItemCount() {

            return mAllPrivilegeList.size() > 0 ? mAllPrivilegeList.size() : 0;
        }

        class MyViewHolder extends RecyclerView.ViewHolder {
            @Bind(R.id.tv_picture)
            ImageView mIvPicture;
            @Bind(R.id.tv_title)
            TextView mTvMsgTitle;

            public MyViewHolder(View itemView) {
                super(itemView);
                ButterKnife.bind(this, itemView);
            }
        }

    }
}